home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / OS2CL016.ZIP / PMSYS.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-04  |  6.9 KB  |  249 lines

  1. /* 
  2.  
  3.  
  4.     pmsys.h (emx+gcc) 
  5.  
  6.     1995 Giovanni Iachello
  7.     This is freeware software. You can use or modify it as you wish,
  8.     provided that the part of code that I wrote remains freeware.
  9.     Freeware means that the source code must be available on request 
  10.     to anyone.
  11.     You must also include this notice in all files derived from this
  12.     file.
  13.  
  14.  
  15. */
  16. #ifndef _PMSYS_H_
  17. #define _PMSYS_H_
  18.  
  19. #define INCL_DOSPROCESS
  20. #define INCL_DOSSEMAPHORES
  21. #define INCL_WIN
  22. #define OS2EMX_PLAIN_CHAR
  23. #include <os2.h>
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // PMAnchorBlock ancapsulates a HAB and some useful functions
  27. //
  28. //
  29.  
  30. class PMMessageQueue;
  31.  
  32.  
  33. class PMAnchorBlock {
  34. protected:
  35.     HAB hab;
  36. public:
  37.     PMAnchorBlock();
  38.     PMAnchorBlock(HAB ab) { hab=ab; }
  39.     virtual ~PMAnchorBlock();
  40.     virtual void init(int flags=0);
  41.     virtual void uninit(void);
  42.     operator HAB () { return hab; }
  43.  
  44.     MRESULT dispachMsg(PQMSG pqmsg) {
  45.         return WinDispatchMsg (hab, pqmsg);
  46.     }
  47.     BOOL getMsg(PQMSG pqmsg, HWND hwndFilter=0L, ULONG msgFilterFirst=0, ULONG msgFilterLast=0) {
  48.         return WinGetMsg (hab,pqmsg,hwndFilter,msgFilterFirst,msgFilterLast);
  49.     }
  50.     BOOL peekMsg(PQMSG pqmsg, HWND hwndFilter=0L, ULONG msgFilterFirst=0, ULONG msgFilterLast=0, ULONG fl=0) {
  51.         return WinPeekMsg (hab,pqmsg,hwndFilter,msgFilterFirst,msgFilterLast,fl);
  52.     }
  53.  
  54.     POINTL msgPos() { 
  55.         POINTL point;
  56.         WinQueryMsgPos(hab,&point);
  57.         return point;
  58.     }
  59.     ULONG msgTime() {
  60.         return WinQueryMsgTime(hab);
  61.     }
  62.  
  63.     LONG loadMessage(ULONG id,LONG cchMax,PSZ pchBuffer,HMODULE hmod=NULLHANDLE)
  64.         { return WinLoadMessage(hab,hmod,id,cchMax,pchBuffer); }
  65.     LONG loadString(ULONG id,LONG cchMax,PSZ pchBuffer,HMODULE hmod=NULLHANDLE)
  66.         { return WinLoadString(hab,hmod,id,cchMax,pchBuffer); }
  67.     BOOL intersectRect(PRECTL prclDst, RECTL *prclSrc1, RECTL *prclSrc2) 
  68.         { return WinIntersectRect (hab,prclDst,prclSrc1,prclSrc2); }
  69.     BOOL ptInRect(RECTL *prcl, POINTL *pptl)
  70.         { return WinPtInRect (hab,prcl,pptl); }
  71.     BOOL unionRect(PRECTL prclDst,RECTL *prclSrc1, RECTL *prclSrc2)
  72.         { return WinUnionRect (hab,prclDst,prclSrc1,prclSrc2); }
  73.  
  74.     BOOL releaseHook (HMQ hmq, LONG iHook, PFN pfnHook, HMODULE hmod)
  75.         { return WinReleaseHook (hab, hmq, iHook, pfnHook, hmod); }
  76.     BOOL setHook (HMQ hmq, LONG iHook, PFN pfnHook, HMODULE hmod) 
  77.         { return WinSetHook (hab, hmq, iHook, pfnHook, hmod); }
  78.     
  79. };
  80.  
  81. //
  82. //
  83. //
  84.  
  85. class PMMessageQueue {
  86. protected:
  87.     HMQ hmq;
  88. public:
  89.     PMMessageQueue();
  90.     operator HMQ () { return hmq; }
  91.     void create(PMAnchorBlock ab) 
  92.         { hmq = WinCreateMsgQueue (ab, 0); }
  93.     BOOL destroy(void) 
  94.         { return WinDestroyMsgQueue(hmq); }
  95. };
  96.  
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99.  
  100. class PMApp {
  101.     int argc;      // conserva anche argomenti | store command line args
  102.     char** argv;   // della linea di comando   |
  103.     PMAnchorBlock ab;
  104.     PMMessageQueue mq;
  105. public:
  106.     PMApp(PMAnchorBlock iab,PMMessageQueue imq,int iargc=0,char** iargv=NULL);
  107.     virtual void quit(int ret=0);    
  108.     virtual void run(void);
  109. };
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112. // PMIniProfile (profile access file)
  113. //
  114.  
  115. class PMIniProfile {
  116. protected:
  117.     HINI hini;
  118. public:
  119.     PMIniProfile(HAB ab,PCSZ filename) {
  120.         hini=PrfOpenProfile(ab,filename);
  121.     }
  122.     PMIniProfile(HINI ihini) {
  123.         hini=ihini;
  124.     }
  125.     virtual ~PMIniProfile()    {
  126.         PrfCloseProfile(hini);
  127.     }
  128.     operator HINI () { return hini; }
  129.     LONG queryInt(PCSZ pszApp, PCSZ pszKey, LONG sDefault) {
  130.         return PrfQueryProfileInt (hini,pszApp, pszKey, sDefault);}
  131.     BOOL queryProfile(HAB ab,PPRFPROFILE pPrfProfile) {
  132.         return PrfQueryProfile (ab,pPrfProfile);}
  133.     BOOL queryData (PCSZ pszApp, PCSZ pszKey, PVOID pBuffer, PULONG pulBufferLength) {
  134.         return PrfQueryProfileData (hini, pszApp, pszKey, pBuffer, pulBufferLength); }
  135.     BOOL querySize (PCSZ pszApp, PCSZ pszKey, PULONG pulReqLen) {
  136.         return PrfQueryProfileSize (hini, pszApp, pszKey, pulReqLen); }
  137.     ULONG queryString (PCSZ pszApp, PCSZ pszKey,PCSZ pszDefault, PVOID pBuffer, ULONG ulBufferLength)    {    
  138.         return PrfQueryProfileString (hini, pszApp, pszKey,pszDefault, pBuffer, ulBufferLength);    }
  139.     BOOL reset (HAB ab,PRFPROFILE *pPrfProfile) {
  140.         return PrfReset (ab,pPrfProfile); }
  141.     BOOL writeData (PCSZ pszApp, PCSZ pszKey, CPVOID pData, ULONG ulDataLength) {
  142.         return PrfWriteProfileData (hini, pszApp, pszKey, pData, ulDataLength); }
  143.     BOOL writeString (PCSZ pszApp, PCSZ pszKey, PCSZ pszData) {
  144.         return PrfWriteProfileString ( hini, pszApp, pszKey, pszData); }
  145. };
  146.  
  147. class PMSystemProfile : public PMIniProfile {
  148. public:
  149.     PMSystemProfile() :    PMIniProfile(HINI_SYSTEM) {    }
  150.     ~PMSystemProfile() { } // do not close the system profile
  151. };
  152.  
  153. class PMUserProfile : public PMIniProfile {
  154. public:
  155.     PMUserProfile() : PMIniProfile(HINI_USER) { }
  156.     ~PMUserProfile() { } // do not close the user profile
  157. };
  158.  
  159. /////////////////////////////////////////////////////////////////////////////
  160. // PMThread class.
  161. // control starts in the main function. All thread member functions can
  162. // use data stored in the thread class. (useful for thread instance data)
  163.  
  164. class PMThread {
  165. protected:
  166.     void* arg;
  167.     int stacksize;
  168.     unsigned long id;
  169. public:
  170.     PMThread(void* iarg=NULL,int istacksize=8192);
  171.     virtual ~PMThread();
  172.     virtual void main(void* arg=NULL);
  173.     friend void threadstart(void* arg);
  174.  
  175.     unsigned long ID() { return id; }
  176.     unsigned failed() { return id==(unsigned long)-1; }
  177.  
  178.     void threadStore(void *p);
  179.     void* threadStore();
  180.     ULONG kill() {
  181.         return DosKillThread (id);
  182.     }
  183.     ULONG resume() {
  184.         return DosResumeThread (id);
  185.     }
  186.     ULONG setPriority(ULONG ulScope, ULONG ulClass, LONG lDelta) {
  187.         return DosSetPriority (ulScope,ulClass,lDelta,id);
  188.     }        
  189.     ULONG suspend() {
  190.         return DosSuspendThread (id);
  191.     }
  192.     ULONG wait(ULONG ulWait) {
  193.         return DosWaitThread (&id, ulWait);
  194.     }
  195. };
  196.  
  197. class PMWindowThread : public PMThread, public PMAnchorBlock {
  198. protected:
  199.     int flags;
  200. public:
  201.     PMWindowThread(void* iarg=NULL,int istacksize=32768,int iflags=0);
  202.     ~PMWindowThread();
  203.     void main(void* arg=NULL);
  204. };
  205.  
  206. /////////////////////////////////////////////////////////////////////////////
  207. //
  208. // Event Semaphore Class
  209. // 12/19/1995
  210. //
  211.  
  212. class PMEventSemaphore {
  213.     PCSZ pszName;
  214.     ULONG ulAttr;
  215.     HEV hev;
  216.     ULONG ulCount;
  217. public:
  218.     ULONG rc;
  219.     // create semaphore
  220.     PMEventSemaphore(ULONG iulAttr=DC_SEM_SHARED,BOOL32 fState=FALSE);
  221.     PMEventSemaphore(PCSZ ipszName,ULONG iulAttr=DC_SEM_SHARED,BOOL32 fState=FALSE);
  222.     // open semaphore
  223.     PMEventSemaphore(PCSZ ipszName, PHEV phev); 
  224.     // close semaphore
  225.     ~PMEventSemaphore();
  226.     operator HEV () { return hev; }
  227.     // other functions
  228.     ULONG post() {
  229.         return rc = DosPostEventSem (hev);
  230.     }
  231.     ULONG query() {
  232.         rc = DosQueryEventSem (hev, &ulCount);
  233.         return ulCount;
  234.     }
  235.     ULONG reset() {
  236.         rc = DosResetEventSem (hev, &ulCount);
  237.         return ulCount;
  238.     }
  239.     ULONG wait(ULONG ulTimeout=(ULONG)SEM_INDEFINITE_WAIT) {
  240.         return rc = DosWaitEventSem (hev, ulTimeout);
  241.     }
  242.     ULONG count() {
  243.         return ulCount;
  244.     }
  245. };
  246.  
  247. #endif
  248.  
  249.